package com.alex.controller.test;

import com.alex.controller.test.exception.AlexException;
import com.alex.controller.test.exception.UserNameNotMatchException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class ExceptionTestController {

//    @ExceptionHandler({ArithmeticException.class})
//    public String test1001(Exception e) {
//        System.out.println(e);
//
//        return "test/error";
//    }

//    @ExceptionHandler({ArithmeticException.class})
//    public ModelAndView test1002(Exception e) {
//        System.out.println(e);
//
//        ModelAndView mv = new ModelAndView();
//        mv.setViewName("test/error");
//        mv.addObject("exception", e);
//        return mv;
//    }

    @RequestMapping("/test/exception")
    public String test() {
        return "test/exception";
    }

    @RequestMapping("/test/exception/01/result")
    public String test01(int a, int b, Model model) {
        int result = a/b;
        model.addAttribute("result", result);
        return "test/result";
    }

    @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "测试-MAPPING: 这个请求路径被设计为正常情况下(方法体内不产生异常时)返回一个HTTP异常")
    @RequestMapping("/test/statusCode/01")
    public String test02(@RequestParam("i") int i) {
        if (i == 13) {
            throw  new UserNameNotMatchException();
        }
        if (i == 14) {
            throw  new AlexException();
        }
        System.out.println();
        return "test/success";
    }

    @RequestMapping(value = "/test/statusCode/02", method = RequestMethod.POST)
    public String test03() {
        return "test/success";
    }

    @RequestMapping(value = "/test/statusCode/03")
    public String test04(@RequestParam("index") int index) {
        String[] l = new String[10];
        System.out.println(l[index]);
        return "test/success";
    }
}
